package com.butterfly.vv;  
import java.util.ArrayList;
import java.util.List;

import com.beem.project.beem.R;
import com.butterfly.vv.model.Contact;
import com.butterfly.vv.utils.PhoneUtil;
import com.teleca.jamendo.activity.ArtistActivity;


import android.R.integer;  
import android.app.Activity;  
import android.content.Context;  
import android.content.Intent;
import android.graphics.LinearGradient;  
import android.os.Bundle;  
import android.util.Log;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.widget.AdapterView;  
import android.widget.AdapterView.OnItemClickListener;  
import android.widget.BaseAdapter;  
import android.widget.Button;
import android.widget.CheckBox;  
import android.widget.CompoundButton;  
import android.widget.Toast;  
import android.widget.CompoundButton.OnCheckedChangeListener;  
import android.widget.ListView;  
import android.widget.TextView;  
 
public class SelectedListActivity extends Activity {  
      
    String[] name=new String[]{  
        "zhou","guo","li","wang","yuan"   
    };  
      
      
    private ListView lv;  
    private mAdapter ma;  
    private PhoneUtil phoneUtil;
    private List<Contact> phoneContacts;
    private List<Contact> simContacts;
    Button yes;
    String mjid;
    
    public void phoneNoClickHandler(String phonoNo[]) {
		RegistryResult.phoneNolaunch(this, phonoNo);
    }
    
    public void phoneNoClickHandler(String phonoNo[],String jid) {
		RegistryResult.phoneNolaunch(this, phonoNo,jid);
    }
      
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.phone_multiple_select);  
          
        lv=(ListView)findViewById(R.id.lv3);  
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        if ("com.btf.PHONE".equals(getIntent().getAction())) {
//			phoneUtil = PhoneUtil.getInstance(getContentResolver());
//			phoneContacts = phoneUtil.retrievePhoneContacts();
			//lv.setAdapter(new mAdapter(SelectedListActivity.this,phoneContacts));
        	phoneContacts = (List<Contact>) getIntent().getSerializableExtra("phoneContacts");
        	ma=new mAdapter(SelectedListActivity.this,phoneContacts); 
        } 
        if("com.btf.SIM".equals(getIntent().getAction())) {
        	phoneContacts = (List<Contact>) getIntent().getSerializableExtra("simContacts");
			ma=new mAdapter(SelectedListActivity.this,phoneContacts); 
        }
        
       // new LoginLoadingDialog(this,R.string.are_you_sure, R.string.app_name,phoneUtil).execute("");
          
//        ma=new mAdapter(SelectedListActivity.this,phoneContacts);  
        lv.setAdapter(ma);  
          
        lv.setOnItemClickListener(new lvlistener());  
        
        yes = (Button) findViewById(R.id.yse);
        yes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 int[] selectitem=ma.getSelectedIndex();
				 String[] phonoNo = new String[selectitem.length];
				
				 for (int i = 0; i < selectitem.length; i++) {
					 Log.i("tag", "-->"+selectitem[i]);
					 Log.i("tag", "-->"+phoneContacts.get(selectitem[i]).name+" "+phoneContacts.get(selectitem[i]).number+" "+phoneContacts.get(selectitem[i]).jid);
//					 Intent data = new Intent(getApplicationContext(), LoginUI.class);
//					 data.putExtra("phoneContacts", (ArrayList<Contact>)phoneContacts);
//					 startActivity(data);
					 //SelectedListActivity.this.setResult(RESULT_OK, data);
					// SelectedListActivity.this.finish();
					 phonoNo[i] = phoneContacts.get(selectitem[i]).number;
					 mjid = phoneContacts.get(selectitem[i]).jid;
				 }
				 for (int j = 0; j < phonoNo.length; j++) {
					 Log.i("tag", "gg-->"+phonoNo[j]);
				}
//				 phoneNoClickHandler(phonoNo);
				 phoneNoClickHandler(phonoNo,mjid);
				
			}
		});
          
    }  
      
      
    private class lvlistener implements OnItemClickListener  
    {  
 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position,  
                long id) {  
            // TODO Auto-generated method stub  
            //ma.toogle(position);  
            ViewHolder holder=(ViewHolder) view.getTag();  
            holder.cb.toggle();  
            int[] selectitem=ma.getSelectedIndex();  
            StringBuffer str=new StringBuffer();  
            for(int i=0;i<selectitem.length;i++)  
            {  
                 // str.append(name[selectitem[i]].toString()); 
            	str.append(phoneContacts.get(selectitem[i]).name); 
                  
            }  
            Toast.makeText(SelectedListActivity.this,str,Toast.LENGTH_SHORT).show();  
        }  
 
      
          
    }  
      
      
    private class mAdapter extends BaseAdapter  
    {  
        boolean[] itemStatus;//=new boolean[name.length];  
        private LayoutInflater mInflater;  
        private Context mContext;  
        List<Contact> contacts;
          
//      public void toogle(int position)  
//      {  
//          if(itemStatus[position]==true)  
//          {  
//              itemStatus[position]=false;  
//          }  
//          else {  
//              itemStatus[position]=true;  
//            
//          }  
//            
//            
//          this.notifyDataSetChanged();  
//            
//      }  
          
        public int[] getSelectedIndex()  
        {  
            if(itemStatus==null||itemStatus.length==0)  
            {  
                return new int[0];  
                  
            }  
            else {  
                  
                int counter=0;  
                for(int i=0;i<itemStatus.length;i++)  
                {  
                    if(itemStatus[i]==true)  
                    counter++;  
                }  
                int[] selectedIndex=new int[counter];  
                int index=0;  
                for(int i=0;i<itemStatus.length;i++)  
                {  
                    if(itemStatus[i]==true)  
                    {  
                        selectedIndex[index++]=i;  
                    }  
                }  
                  
                return selectedIndex;  
            }  
        }  
          
        private mAdapter(Context context)  
        {  
            mInflater=LayoutInflater.from(context);  
            this.mContext=context;  
        }  
        public mAdapter(Context context,
				List<Contact> phoneContacts) {
			// TODO Auto-generated constructor stub
        	mInflater=LayoutInflater.from(context);  
            this.mContext=context; 
            this.contacts = phoneContacts;
            itemStatus = new boolean[phoneContacts.size()]; 
		}

		public int getCount() {  
            // TODO Auto-generated method stub  
            //return name.length;  
			return contacts.size();
        }  
 
        @Override 
        public Object getItem(int position) {  
            // TODO Auto-generated method stub  
            return contacts.get(position);  
        }  
 
        @Override 
        public long getItemId(int position) {  
            // TODO Auto-generated method stub  
            return position;  
        }  
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            ViewHolder holder;  
            if(convertView==null||convertView.getTag()==null)  
            {  
                holder=new ViewHolder();  
                convertView=mInflater.inflate(R.layout.phone_list_item,null);  
                holder.tv=(TextView)convertView.findViewById(R.id.tv);  
                holder.cb=(CheckBox)convertView.findViewById(R.id.cb);  
                  
                  
                convertView.setTag(holder);  
            }  
            else {  
                holder=(ViewHolder)convertView.getTag();  
            }  
            //holder.tv.setText(name[position]); 
           // Contact contact = (Contact)this.getItem(position);
            holder.tv.setText(contacts.get(position).name); 
              
            holder.cb.setOnCheckedChangeListener(new listener(position));  
            if(itemStatus[position]==true)  
            {  
                holder.cb.setChecked(true);  
            }  
            if(itemStatus[position]==false)  
            {  
                holder.cb.setChecked(false);  
            }  
            return convertView;  
        }  
          
        private class listener implements OnCheckedChangeListener  
        {  
            private int mposition;  
            public listener(int position)  
            {  
                this.mposition=position;  
            }  
            @Override 
            public void onCheckedChanged(CompoundButton buttonView,  
                    boolean isChecked) {  
                // TODO Auto-generated method stub  
                if(isChecked)  
                {  
                    itemStatus[mposition]=true;  
                }  
                else {  
                    itemStatus[mposition]=false;  
                }  
                  
            }  
              
        }  
          
          
    }  
 
    public class ViewHolder   
    {  
        private TextView tv;  
        private CheckBox cb;  
    }  
} 